/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.nvd;

import jp.go.aist.six.util.castor.CastorDao;
import jp.go.aist.six.vuln.model.scap.core.CheckReference;
import jp.go.aist.six.vuln.model.scap.cpe.CpeName;
import jp.go.aist.six.vuln.model.scap.vulnerability.CweReference;
import jp.go.aist.six.vuln.model.scap.vulnerability.Vulnerability;
import jp.go.aist.six.vuln.model.scap.vulnerability.VulnerabilityReference;
import jp.go.aist.six.vuln.model.scap.vulnerability.VulnerableSoftware;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Collection;
import java.util.Date;



/**
 * @author  Akihito Nakamura, AIST
 * @version $Id: VulnerabilityDao.java 256 2010-06-14 03:58:05Z nakamura5akihito $
 */
public class VulnerabilityDao
    extends CastorDao<Vulnerability>
{

    /**
     * Logger.
     */
    private static Log  _LOG = LogFactory.getLog( VulnerabilityDao.class );



    /**
     * Constructor.
     */
    public VulnerabilityDao()
    {
        super( Vulnerability.class );
//        super( Vulnerability.class, new VulnerabilityHelper() );
    }



    /**
     */
    private void _syncRelated(
                    final Vulnerability vuln
                    )
    {
        VulnerableSoftware  softlist = vuln.getVulnerableSoftwareList();
        if (softlist != null) {
            Collection<CpeName>  productlist = softlist.getProduct();
            if (productlist != null  &&  productlist.size() > 0) {
                Collection<CpeName>  p_productlist =
                    getForwardingDao( CpeName.class ).syncAll( productlist );
                softlist.setProduct( p_productlist );
            }
        }

        Collection<CheckReference>  assesslist = vuln.getAssessmentCheck();
        if (assesslist != null) {
            Collection<CheckReference>  p_assesslist =
                getForwardingDao( CheckReference.class ).syncAll( assesslist );
            vuln.setAssessmentCheck( p_assesslist );
        }

        Collection<CweReference>  cwelist = vuln.getCwe();
        if (cwelist != null) {
            Collection<CweReference>  p_cwelist =
                getForwardingDao( CweReference.class ).syncAll( cwelist );
            vuln.setCwe( p_cwelist );
        }

        Collection<VulnerabilityReference>  reflist = vuln.getReferences();
        if (reflist != null) {
            Collection<VulnerabilityReference>  p_reflist =
                getForwardingDao( VulnerabilityReference.class ).syncAll( reflist );
            vuln.setReferences( p_reflist );
        }
    }



    //**************************************************************
    //  Dao, CastorDao
    //**************************************************************

    @Override
    public Object create(
                    final Vulnerability vuln
                    )
    {
        if (_LOG.isInfoEnabled()) {
            _LOG.info( "create Vulnerability: id=" + vuln.getID() );
        }
        _syncRelated( vuln );
        return super.create( vuln );
    }



    @Override
    public Vulnerability sync(
                    final Vulnerability vuln
                    )
    {
        if (_LOG.isInfoEnabled()) {
            _LOG.info( "sync Vulnerability: " + vuln.getID() );
        }
        Vulnerability  p_vuln = super.sync( vuln );

        if (p_vuln != vuln) {
            if (_LOG.isTraceEnabled()) {
                _LOG.trace( "vulnerability already exists: id=" + vuln.getID() );
            }
            Date  lastModifiedDatetime = vuln.getLastModifiedDatetime();
            if (lastModifiedDatetime != null) {
                Date  p_lastModifiedDatetime = p_vuln.getLastModifiedDatetime();
                if (p_lastModifiedDatetime == null
                                ||  lastModifiedDatetime.compareTo( p_lastModifiedDatetime ) > 0) {
                    if (_LOG.isTraceEnabled()) {
                        _LOG.trace( "existing vulnerability is obsolete: id=" + vuln.getID() );
                    }
                    // vuln is newer than p_vuln
                    p_vuln.setVulnerableSoftwareList( vuln.getVulnerableSoftwareList() );
                    p_vuln.setLastModifiedDatetime( lastModifiedDatetime );
                    p_vuln.setCvss( vuln.getCvss() );
                    p_vuln.setSecurityProtection( vuln.getSecurityProtection() );
                    p_vuln.setAssessmentCheck( vuln.getAssessmentCheck() );
                    p_vuln.setCwe( vuln.getCwe() );
                    p_vuln.setReferences( vuln.getReferences() );
                    p_vuln.setSummary( vuln.getSummary() );

                    _syncRelated( p_vuln );
                }
            }
        }

        return p_vuln;
    }

}
// VulnerabilityDao
